package com.example.kingdee.controller;

import com.example.kingdee.common.AjaxResult;
import com.example.kingdee.common.StringUtils;
import com.example.kingdee.config.RuoYiConfig;
import com.example.kingdee.dto.EntryIds;
import com.example.kingdee.dto.JuShuiTanDto;
import com.example.kingdee.dto.PurchaseSyncDto;
import com.example.kingdee.dto.SyncDto;
import com.example.kingdee.exception.ProcedureException;
import com.example.kingdee.service.JuShuiTanService;
import com.example.kingdee.util.JuShuiTanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 聚水潭服务层
 */
@RestController
@RequestMapping("//juShuiTan")
public class JuShuiTanController {

    @Autowired
    private RuoYiConfig ruoYiConfig;

    @Autowired
    private JuShuiTanService juShuiTanService;



    /**
     * 获取token
     *
     * @return
     */
    @GetMapping("/getAccessToken")
    public AjaxResult getAccessToken() {
        String accessToken = juShuiTanService.getAccessToken();
        if (StringUtils.isNull(accessToken)) {
            throw new ProcedureException("获取token失败");
        }
        return AjaxResult.success(accessToken);
    }

    /**
     * 聚水潭单据批量同步
     * @return
     */
    @PostMapping("/jstBillSync")
    public AjaxResult jstBillSync(@RequestBody SyncDto syncDto) {
        //处理单据编号
        List<EntryIds> entryIds = syncDto.getEntryIds();
        if (entryIds == null || entryIds.size() ==0){
            return AjaxResult.error("请选择数据");
        }
        List<String> poIds = new ArrayList<>();
        entryIds.forEach(e -> poIds.add(e.getEntryId()));
        //制定查询状态
        List<String> status = new ArrayList<>();
        status.add("sent");//已发货
        syncDto.setStatus(status);
        syncDto.setPoIds(poIds);
        //填充时间段
        syncDto.setStartTimeType(Calendar.DATE);
        syncDto.setEndTimeType(Calendar.DATE);
        syncDto.setStart(-6);
        syncDto.setEnd(1);

        String result = juShuiTanService.jstBillSync(syncDto);
        if (result.equals("200")) {
            return AjaxResult.success("添加成功");
        }else {
            return AjaxResult.error(result);
        }
    }

    /**
     * 聚水潭单据批量同步
     * @return
     */
    @PostMapping("/jstReFundBillSync")
    public AjaxResult jstReFundBillSync(@RequestBody SyncDto syncDto) {
        //处理单据编号
        List<EntryIds> entryIds = syncDto.getEntryIds();
        if (entryIds == null || entryIds.size() ==0){
            return AjaxResult.error("请选择数据");
        }
        List<String> poIds = new ArrayList<>();
        entryIds.forEach(e -> poIds.add(e.getEntryId()));
        //制定查询状态
        List<String> status = new ArrayList<>();
        status.add("Confirmed");//已确认
        syncDto.setStatus(status);
        syncDto.setPoIds(poIds);
        //填充时间段
        syncDto.setStartTimeType(Calendar.DATE);
        syncDto.setEndTimeType(Calendar.DATE);
        syncDto.setStart(-6);
        syncDto.setEnd(1);

        String result = juShuiTanService.jstBillSync(syncDto);
        if (result.equals("200")) {
            return AjaxResult.success("添加成功");
        }else {
            return AjaxResult.error(result);
        }
    }

    /**
     * 云星空单据上传
     * @return
     */
    @PostMapping("/kindeeBillSync")
    public AjaxResult kindeeBillSync(@RequestBody SyncDto syncDto) {
        //处理单据编号
        List<EntryIds> entryIds = syncDto.getEntryIds();
        if (entryIds == null || entryIds.size() ==0){
            return AjaxResult.error("请选择数据");
        }
        List<String> poIds = new ArrayList<>();
        entryIds.forEach(e -> poIds.add(e.getEntryId()));
        syncDto.setPoIds(poIds);

        String result = juShuiTanService.kindeeBillSync(syncDto);
        if (result.equals("200")) {
            return AjaxResult.success("添加成功");
        }else {
            return AjaxResult.error(result);
        }
    }

}
